package com.journaldev.spring.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.journaldev.spring.model.PersonalOffice;

@Transactional
public class PersonalOfficeDAOImpl implements PersonalOfficeDAO {
	
	private static final Logger logger = LoggerFactory.getLogger(DistrictDAOImpl.class);

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void addOffice(PersonalOffice p) {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();
		session.persist(p);
		logger.info("Personal office saved successfully, office Details="+p);
	}

	@Override
	public void updateOffice(PersonalOffice p) {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();
		session.update(p);
		logger.info("Personal office updated successfully, office Details="+p);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PersonalOffice> listOffice() {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();
		List<PersonalOffice> officeList = session.createQuery("from PersonalOffice").list();
		for(PersonalOffice p : officeList){
			logger.info("PersonalOffice List::"+p);
		}
		return officeList;
	}

	@Override
	public PersonalOffice getOfficeById(int id) {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();		
		PersonalOffice p = (PersonalOffice) session.get(PersonalOffice.class, new Integer(id));
		logger.info("PersonalOffice loaded successfully, PersonalOffice details="+p);
		return p;
	}

	@Override
	public void removeOffice(int id) {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();
		PersonalOffice p = (PersonalOffice) session.get(PersonalOffice.class, new Integer(id));
		if(null != p){
			session.delete(p);
		}
		logger.info("PersonalOffice deleted successfully, PersonalOffice details="+p);
	}

}
